package com.coolnie.service;

import com.coolnie.dao.UsersMapper;
import com.coolnie.entity.Pager;
import com.coolnie.entity.User;
import com.coolnie.entity.Users;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/6/15 0015.
 */
@Service("usersService1")
public class UsersServiceImpl1 implements UsersService{
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<Users> findAll() {
        return usersMapper.findAll();
    }

    @Override
    public int insert(Users record) {
        return usersMapper.insert(record);
    }

    @Override
    public Users findOne(@Param("username") String username) {
        return usersMapper.findOne(username);
    }

    @Override
    public int insertSelective(Users record) {
        return usersMapper.insertSelective(record);
    }

    @Override
    public int deleted(@Param("username") String username) {
        return usersMapper.deleted(username);
    }

    @Override
    public int update(Users record) {
        return usersMapper.update(record);
    }

    /**
     *
     * @param pageIndex
     * @return
     */
    @Override
    public Pager<Users> findAllByPageIndex(int pageIndex) {
        int pageSize = 6;
        int pageOffset = (pageIndex - 1) * pageSize;
        int count = usersMapper.count();    // 总共的记录数
        //计算总页数 如果页数整除，就显示正确页数，如果不能整除，就对页面加1
        int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize +1;
        //把获得的页数存放在new的集合 userse里
        List<Users> userses = usersMapper.findAllByPage(pageOffset, pageSize);

        Pager<Users> usersPager = new Pager<>();
        usersPager.setDatas(userses);
        usersPager.setPageIndex(pageIndex);
        usersPager.setAllPage(totalPage);
        return usersPager;
    }
}
